package com.yellowpage.omc.o2o.service;

import java.util.List;
import java.util.Map;

import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import com.yellowpage.white.common.Constants;
import com.yellowpage.white.common.PageRender;
import com.yellowpage.white.dao.QueryAssistantSQL;
import com.yellowpage.white.service.BaseServiceImpl;

import com.yellowpage.omc.o2o.data.Channel;
import com.yellowpage.omc.o2o.dao.ChannelDao;

public class ChannelServiceImpl extends BaseServiceImpl implements ChannelService {
	  private ChannelDao channelDao;
	
	  public void setChannelDao(ChannelDao channelDao) {
		  this.channelDao = channelDao;
	  }
	
	
	  /* Smooth generate Method */ 
	  public Channel getChannel(Long id){
	      return channelDao.get(id);
	  }
	  
    public List<Channel> getAllChannel(){
        return channelDao.getAll();
    }
    
    public List<Map> queryChannelByPage(Map queryMap, PageRender pageRender) {		
        
		QueryAssistantSQL sqlAssistant = baseDao.newQueryAssistantSQL(
				"SELECT a.CHANNEL_ID , a.LAST_ORDER , a.URL , (select display_name from CA_USER where user_id = a.CREATE_USER) as CREATE_USER , a.CREATE_TIME , (select display_name from CA_USER where user_id = a.LAST_UPDATE_USER) as LAST_UPDATE_USER , a.LAST_UPDATE_TIME , a.NOTE , a.NAME , a.STATUS    FROM OMC_CHANNEL a WHERE 1=1 ")
				    			.addCondition(" and CHANNEL_ID = :channelId ")
							    			.addCondition(" and LAST_ORDER = :lastOrder ")
							    			.addCondition(" and URL = :url ")
							    			.addCondition(" and CREATE_USER = :createUser ")
							    		    .addCondition(" and date_format(CREATE_TIME,'%Y-%m-%d') =  :createTime  ")
		    				    			.addCondition(" and LAST_UPDATE_USER = :lastUpdateUser ")
							    		    .addCondition(" and date_format(LAST_UPDATE_TIME,'%Y-%m-%d') =  :lastUpdateTime  ")
		    				    			.addCondition(" and NOTE = :note ")
							    			.addCondition(" and NAME = :name ")
							    			.addCondition(" and STATUS = :status ")
							    ; 		    
		    

		List<Map> resultList = sqlAssistant.queryByPage(
				baseDao.getConnection(), queryMap, pageRender);

		return resultList;
	}
    
    
    public void saveChannel(Channel channel){
        channelDao.save(channel);
    }
    
	  public void updateChannel(Channel channel){
	      channelDao.update(channel);
	  }
	  
	  public void delete(List deleteList){
	      channelDao.remove(deleteList);
	  }
	  
	  public List<Map> getO2OStatistics(Map queryMap){
		  
			QueryAssistantSQL sqlAssistant = baseDao.newQueryAssistantSQL(
					"select b.REQUEST_TYPE, a.COMPANY, a.APP_NAME, a.TYPE, a.STATUS, " +
					"       sum(b.COUNT) as 'ALLSUM',  " +
					"       sum(IF(b.RESPONSE_CODE='000000',b.COUNT,0)) as VALIDCOUNT, " +
					"       sum(IF(b.RESPONSE_CODE!='000000',b.COUNT,0)) as INVALIDCOUNT, " +
					"       sum(IF(b.RESPONSE_CODE='000000' and b.RESPONSE_CONTENT is null,b.COUNT,0)) as PAYUSERCOUNT, " +
					"       sum(IF(b.RESPONSE_CODE!='000000' and b.RESPONSE_CONTENT is null,b.COUNT,0)) as BDCDATACOUNT " +
					"  from NSP_LOG_SUM_O2O b, NSP_REQUEST_TYPE a" +
					" where b.REQUEST_TYPE = a.CODE ")
					    			.addCondition(" and b.LOG_ID = :logId ")
								    			//.addCondition(" and SUBSTR(b.LOG_TIME FROM 1 FOR 10) = :requestTime ")
					    						.addCondition(" and SUBSTR(b.LOG_TIME FROM 1 FOR 10) >= :requestTime ")
					    						.addCondition(" and SUBSTR(b.LOG_TIME FROM 1 FOR 10) <= :requestTimeEnd ")
								    			.addCondition(" and b.REQUEST_INFO = :requestInfo ")
								    			.addCondition(" and b.REQUEST_TYPE = :requestType ")
								    			.addCondition(" and b.REQUEST_IP = :requestIp ")
								    			.addCondition(" and b.RESPONSE_DATA = :responseData ")
								    			.addCondition(" and b.RESPONSE_INFO = :responseInfo ")
								    			.addCondition(" and b.ID = :id ")
								    			.addCondition(" and b.RESPONSE_CODE = :responseCode ")
								    ; 		    
			    

			sqlAssistant.setTail(" group by b.REQUEST_TYPE ");
			
			List<Map> resultList = sqlAssistant.query(
					baseDao.getConnection(), queryMap);

			return resultList;
	  }
	 
	  public List<Map> getO2OLogerSum(Map queryMap, PageRender pageRender){
		  
		  QueryAssistantSQL sqlAssistant = baseDao
					.newQueryAssistantSQL(
							"SELECT a.LOG_SUM_ID , a.SERVER_ID , a.LOG_FILE_NAME , a.LOG_TIME , a.REQUEST_TYPE , a.REQUEST_CONTENT , a.RESPONSE_CONTENT , a.CREATE_USER , a.CREATE_TIME , a.NOTE , a.RESPONSE_CODE , a.COUNT , a.STATUS    FROM NSP_LOG_SUM_O2O a WHERE 1=1 ")
					.addCondition(" and LOG_SUM_ID = :logSumId ")
					.addCondition(" and SERVER_ID = :serverId ")
					.addCondition(" and LOG_FILE_NAME = :logFileName ")
					.addCondition(" and LOG_TIME = :logTime ")
					.addCondition(" and REQUEST_TYPE = :requestType ")
					.addCondition(" and REQUEST_CONTENT = :requestContent ")
					.addCondition(" and RESPONSE_CONTENT = :responseContent ")
					.addCondition(" and CREATE_USER = :createUser ")
					.addCondition(
							" and date_format(CREATE_TIME,'%Y-%m-%d') =  :createTime  ")
					.addCondition(" and NOTE = :note ")
					.addCondition(" and RESPONSE_CODE = :responseCode ")
					.addCondition(" and COUNT = :count ")
					.addCondition(" and STATUS = :status ");

			List<Map> resultList = sqlAssistant.queryByPage(
					baseDao.getConnection(), queryMap, pageRender);

			return resultList;
	  }
	
}	